﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using DBUtility;

namespace DAL
{
   public class RegistrateDAL
    {

        //制造登记不良
       public bool Registrate(string FCARD, float lf_Weight, string Reason, string CREATOR, string CREATETIME)
        {
            bool result = false;
            StringBuilder sql = new StringBuilder();
            Dictionary<string, object> dic = new Dictionary<string, object>();

            string DeviceNo = FCARD.Substring(0, 2).ToString().Trim();
            string Spec = FCARD.Substring(2, 10).ToString().Trim();
            string ProMonth = FCARD.Substring(12, 1).ToString().Trim();
            string ProDay = FCARD.Substring(13, 1).ToString().Trim();
            string OrderBatch =FCARD.Substring(14, 1).ToString().Trim();

            string TrayNo =FCARD.Substring(15, 3).ToString().Trim();
            string TrayBatch =FCARD.Substring(18, 1).ToString().Trim();
            string ExpireMonth = FCARD.Substring(19, 1).ToString().Trim();
            string ExpireDay = FCARD.Substring(20, 1).ToString().Trim();
            string ProTime =FCARD.Substring(21, 4).ToString().Trim();

            int ID = GegID();
            if (ID != -1)
            {
                sql.Append(" insert into harmful_ledger(ID,FID,DeviceNo,Spec,ProMonth,ProDay,");
                sql.Append(" OrderBatch,TrayNo,TrayBatch,ExpireMonth,ExpireDay,");
                sql.Append(" ProTime,Weight,Reason,CREATOR,CREATETIME,STATE)");
                sql.Append("  values('" + ID + "','" + FCARD + "','" + DeviceNo + "','" + Spec + "','" + ProMonth + "',");
                sql.Append(" '" + ProDay + "','" + OrderBatch + "','" + TrayNo + "',");
                sql.Append(" '" + TrayBatch + "','" + ExpireMonth + "','" + ExpireDay + "',");
                sql.Append(" '" + ProTime + "',nvl('" + lf_Weight + "',0),'" + Reason + "',");
                sql.Append(" '" + CREATOR + "',to_date('" + CREATETIME + "','YYYY-MM-DD HH24:MI:SS'),'0')");

                try
                {
                    int res = DbHelperOra.ExecuteSql(sql.ToString());
                    if (res > 0)
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    dic.Add("msg", ex.Message);
                    dic.Add("masterid", CREATOR);

                    InsertEventLog(dic);
                }
            }
            else
            {
                result = false;
            }
            return result;
        }

        //获取 Harmful_Ledger主键ID
        public int GegID()
        {
            DataSet ds = new DataSet();
            StringBuilder sql = new StringBuilder();
            int i = -1;
            sql.Append("select SEQ_HARMFUL_LEDGER.nextval id from dual");

            try
            {
                ds = DbHelperOra.Query(sql.ToString());
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        i = int.Parse(dr["id"].ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return i;
        }

       //技术更新NG状态
        public bool UpdateStateNG(string FCARD, string UPDATER, string UPDATETIME)
        {
            bool result = false;
            StringBuilder sql = new StringBuilder();
            Dictionary<string, object> dic = new Dictionary<string, object>();
            sql.Append("update harmful_ledger set state = '-2',UPDATER ='" + UPDATER + "',UPDATETIME=to_date('" + UPDATETIME + "','YYYY-MM-DD HH24:MI:SS') where fid ='" + FCARD + "' and state = 2 ");
            try
            {
                int i = DbHelperOra.ExecuteSql(sql.ToString());
                if (i > 0)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                dic.Add("msg", ex.Message);
                dic.Add("masterid", UPDATER);

                InsertEventLog(dic);
            }
            return result;
        }

        //技术更新OK状态
        public bool UpdateStateOK(string FCARD, string UPDATER, string UPDATETIME)
        {
            bool result = false;
            StringBuilder sql = new StringBuilder();
            Dictionary<string, object> dic = new Dictionary<string, object>();
            sql.Append("update harmful_ledger set state = '3',UPDATER ='" + UPDATER + "',UPDATETIME=to_date('" + UPDATETIME + "','YYYY-MM-DD HH24:MI:SS') where fid ='" + FCARD + "' and state = 2");
            try
            {
                int i = DbHelperOra.ExecuteSql(sql.ToString());
                if (i > 0)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                //throw ex;
                dic.Add("msg", ex.Message);
                dic.Add("masterid", UPDATER);

                InsertEventLog(dic);
            }
            return result;
        }

       //技术校验F卡，查询有没有F卡号
        public bool GetFCARD(string FCARD)
        {
            int i = 0;
            bool lb_return = false;
            DataSet ds = new DataSet();
            StringBuilder sql = new StringBuilder();
            sql.Append("select count(0) num from harmful_ledger where fid ='" + FCARD + "'");
            try
            {
                ds = DbHelperOra.Query(sql.ToString());
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        i = int.Parse(dr["num"].ToString());
                        if (i > 0)
                        {
                            lb_return = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return lb_return;
        }

       //技术部获取待扫码的F卡
        public DataSet GetFCARDList()
        {
            DataSet ds = new DataSet();
            StringBuilder sql = new StringBuilder();
            sql.Append("select  ROWNUM, FID from harmful_ledger t where  state = '2'");
            try
            {
                ds = DbHelperOra.Query(sql.ToString());

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ds;
        }


        #region 插入日志
        //插入日志
        public bool InsertEventLog(IDictionary<string, object> where)
        {
            bool result = false;
            StringBuilder sql = new StringBuilder();
            string id = GetMaxId().ToString();
            string msg = string.Empty;
            string masterid = string.Empty;
            #region 处理数据
            if (where.ContainsKey("msg") && where["msg"] != null && where["msg"].ToString() != "")
            {
                msg = where["msg"].ToString().Trim();
            }
            if (where.ContainsKey("masterid") && where["masterid"] != null && where["masterid"].ToString() != "")
            {
                masterid = where["masterid"].ToString().Trim();
            }
            #endregion
            sql.Append("insert into eventlog t values('" + id + "','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", System.Globalization.DateTimeFormatInfo.InvariantInfo) + "','" + msg + "','" + masterid + "')");
            try
            {
                int i = DbHelperOra.ExecuteSql(sql.ToString());
                //int i = this.DbHelperInstance.ExecuteNonQuery(CommandType.Text, sql);
                if (i == 1)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }


        /// <summary>
        /// 获取最大的id
        /// </summary>
        /// <returns>返回最大的id+1</returns>
        public int GetMaxId()
        {
            int maxid = 0;
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("select max(to_number(id))+1 from eventlog t");
                maxid = DbHelperOra.GetMaxID("id", "eventlog");
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return maxid;
        }

  
    #endregion
    }
}
